Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix macro hygiene bug #32923

Merged
merged 4 commits into from
Apr 16, 2016
Merged

Fix macro hygiene bug #32923

merged 4 commits into from
Apr 16, 2016

Conversation

jseyfried
Copy link
Contributor

@jseyfried jseyfried commented Apr 13, 2016

This fixes #32922 (EDIT: also #31856, #10681, and #26223), macro hygiene bugs.
It is a [breaking-change]. For example, the following would break:

fn main() {
    let x = true;
    macro_rules! foo { () => {
        let x = 0;
        macro_rules! bar { () => {x} }
        let _: bool = bar!();
        //^ `bar!()` used to resolve the first `x` (a bool),
        //| but will now resolve to the second x (an i32).
    }}
    foo! {};
}

r? @nrc

@jseyfried jseyfried changed the title Fix hygiene Fix macro hygiene bug Apr 13, 2016
@jseyfried
Copy link
Contributor Author

This could use a crater run (cf. discussion in #32922).

@alexcrichton
Copy link
Member

Starting a crater run

@alexcrichton alexcrichton added the S-waiting-on-crater Status: Waiting on a crater run to be completed. label Apr 14, 2016
@alexcrichton
Copy link
Member

Crater reports one regression, but it looks spurious, so essentially nothing from crater.

@jseyfried
Copy link
Contributor Author

Great, thanks!

@nrc
Copy link
Member

nrc commented Apr 15, 2016

Could you add some of the tests from #31856 please? r+ with that

@jseyfried
Copy link
Contributor Author

@nrc I added another test from #31856.

@nrc
Copy link
Member

nrc commented Apr 15, 2016

@bors: r+

@bors
Copy link
Contributor

bors commented Apr 15, 2016

📌 Commit ca1d29c has been approved by nrc

Manishearth added a commit to Manishearth/rust that referenced this pull request Apr 15, 2016
Fix macro hygiene bug

This fixes rust-lang#32922 (EDIT: and fixes rust-lang#31856), macro hygiene bugs.
It is a [breaking-change]. For example, the following would break:
```rust
fn main() {
    let x = true;
    macro_rules! foo { () => {
        let x = 0;
        macro_rules! bar { () => {x} }
        let _: bool = bar!();
        //^ `bar!()` used to resolve the first `x` (a bool),
        //| but will now resolve to the second x (an i32).
    }}
    foo! {};
}
```

r? @nrc
bors added a commit that referenced this pull request Apr 15, 2016
Rollup of 11 pull requests

- Successful merges: #32923, #32926, #32929, #32931, #32935, #32945, #32946, #32964, #32970, #32973, #32997
- Failed merges:
@bors bors merged commit ca1d29c into rust-lang:master Apr 16, 2016
@jseyfried
Copy link
Contributor Author

Fixes #10681.

@jseyfried
Copy link
Contributor Author

Fixes #26223.

@bluss bluss added relnotes Marks issues that should be documented in the release notes of the next release. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Jun 9, 2016
bors added a commit that referenced this pull request Jun 23, 2016
Fix macro hygiene regression

The regression was caused by #32923, which is currently in beta.

The following is an example of regressed code:
```rust
fn main() {
    let x = 0;
    macro_rules! foo { () => {
        println!("{}", x); // prints `0` on stable and after this PR, prints `1` on beta and nightly
    } }

    let x = 1;
    foo!();
}
```

For code to regress, the following is necessary (but not sufficient):
 - There must be a local variable before a macro in a block, and the macro must use the variable.
 - There must be a second local variable with the same name after the macro.
 - The macro must be invoked in a statement position after the second local variable.

For example, if the `let x = 0;` from the breaking example were commented out, it would (correctly) not compile on beta/nightly. If the semicolon were removed from `foo!();`, it would (correctly) print `0` on beta and nightly.

r? @nrc
@jseyfried jseyfried deleted the fix_hygiene branch February 12, 2017 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes Marks issues that should be documented in the release notes of the next release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Macro hygiene bug
5 participants